package com.pro.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.beans.Alarm;
import com.pro.beans.AlarmTypeResult;
import com.pro.beans.Page;
import com.pro.beans.Pie;
import com.pro.mybatis.ListPage;
import com.pro.param.RequestParam;
import com.pro.param.ResponseParam;
import com.pro.service.AlarmService;

@Controller
public class AlarmController extends BaseController{

	@Resource
	public AlarmService alarmService;
	
	@RequestMapping("loadAlarmList.json")
	@ResponseBody
	public List<Alarm> loadAlarmList(){
		List<Alarm> alarmList = alarmService.getObjectsByPage(new Page());
//		FormattingConversionServiceFactoryBean
		return alarmList;
	}

	@RequestMapping("delAlarm.do")
	public String delAlarm(Model model,@org.springframework.web.bind.annotation.RequestParam("id")Integer id){
		alarmService.deleteAlarm(id);
		this.setSuccess(true);
		this.setMsg("删除成功");
		model.addAttribute("menuId","1");
		model.addAttribute("jspName","alarmList");
		return "index";
	}

	@RequestMapping("updateAlarm.do")
	public String updateAlarm(HttpServletRequest request,Model model){
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		String resulType= request.getParameter("resulType");
		if(type==null || type.equals("") ||id==null || id.equals("") ){
			model.addAttribute("msg","设置失败(请选择一项)");
		}else{
			Alarm alarm = new Alarm();
			alarm.setId(Integer.valueOf(id));
			alarm.setResult(resulType);
			alarmService.update(alarm);
			model.addAttribute("msg","操作成功!");
		}
		String name = (String)request.getParameter("jspName");
		model.addAttribute("type", type);
		model.addAttribute("jspName", name);
		return "index";
	}
	
	
	@RequestMapping("loadAlarmListBySearch.json")
	@ResponseBody
	public ResponseParam<Alarm> loadAlarmListBySearch(@ModelAttribute("page")Page page,@ModelAttribute("requestParam")RequestParam requestParam){
		ListPage listPage = alarmService.loadAlarmList(requestParam);
		this.setData(listPage);
		this.setSuccess(true);
		return this.getResponseParam();
	}
	@RequestMapping("loadAlarmListByType.json")
	@ResponseBody
	public ResponseParam<AlarmTypeResult> loadAlarmListByType(){
		List<AlarmTypeResult> alarmList = alarmService.searchAlarmByType();
//		responseParam.setDataList(alarmList);
		this.setDataList(alarmList);
		this.setSuccess(true);
		return this.getResponseParam();
	}
	
	@RequestMapping("addAlarm.json")
	@ResponseBody
	public ResponseParam addAlarm(@ModelAttribute("alarm")Alarm alarm,@org.springframework.web.bind.annotation.RequestParam(value="id",required=false)Integer id){
		if(id!=null){
			alarmService.update(alarm);
		}else{
			alarmService.addAlarm(alarm);
		}
		this.setMsg("操作成功");
		return this.getResponseParam();
	}
	@RequestMapping("loadAlarmById.json")
	@ResponseBody
	public ResponseParam loadAlarmById(@org.springframework.web.bind.annotation.RequestParam("id")Integer id){
		Alarm alarm = alarmService.getAlarmById(id);
		this.setMsg("查找成功");
		this.setData(alarm);
		this.setSuccess(true);//.setData(true);
		return this.getResponseParam();
	}
	@RequestMapping("deleteAlarmById.json")
	@ResponseBody
	public ResponseParam deleteAlarmById(@ModelAttribute("id")Integer id){
		int i = alarmService.deleteAlarm(id);
		this.setSuccess(true);
		this.setMsg("删除成功");
		return this.getResponseParam();
//		return 
	}
	@RequestMapping("loadDataByDays.json")
	@ResponseBody
	public ResponseParam loadDataByDays(){
		int[][] array = alarmService.loadDataByDays();
		List<Pie> pieList = alarmService.loadPie();
		this.setSuccess(true);
		this.setMsg("查询成功");
		this.setArray(array);
		this.setDataList(pieList);
		this.setData(null);
		return this.getResponseParam();
//		return 
	}
	@RequestMapping("loadAlarmByType.json")
	@ResponseBody
	public ResponseParam loadAlarmByType(){
		List<Alarm> pieList = alarmService.loadAlarmByType();
		this.setSuccess(true);
		this.setMsg("查询成功");
		this.setDataList(pieList);
		this.setData(null);
		return this.getResponseParam();
//		return 
	}
}
